package com.example.livingadminmaster.controller;

import com.example.livingadminmaster.common.R;
import com.example.livingadminmaster.pojo.Faculty;
import com.example.livingadminmaster.pojo.StatusRequest;
import com.example.livingadminmaster.service.FacultyService;
import lombok.Getter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//@CrossOrigin
@RestController
@RequestMapping("/faculty")
public class FacultyController {
    @Autowired
    private FacultyService facultyService;

    /**
     * 查询所有的教职工信息
     *
     * @return
     */
    @ResponseBody
    @GetMapping("/AllFaculties")
    public List<Faculty> getAllFaculties() {
        List list = facultyService.getAllFaculties();
        return list;
    }

    /**
     * 根据id查询单个教职工信息
     *
     * @param faculty_id
     * @return
     */
    @GetMapping("/{faculty_id}")
    public Faculty getFacultyId(@PathVariable long faculty_id) {
        return facultyService.getFacultyId(faculty_id);
    }

    /**
     * 根据用户名查询单个教职工信息
     *
     * @param username
     * @return
     */
    @GetMapping("/selectByUserName")
    public Faculty getFacultyUsername(@RequestParam String username) {
        return facultyService.getFacultyUsername(username);
    }

    /**
     * 根据性别查询单个教职工信息
     *
     * @param gender
     * @return
     */
    @GetMapping("/selectByGender")
    public List<Faculty> getFacultyGender(@RequestParam boolean gender) {
        return facultyService.getFacultyGender(gender);
    }

    /**
     * 根据部门查询单个教职工信息
     *
     * @param department
     * @return
     */
    @GetMapping("/selectByDepartment")
    public List<Faculty> getDepartment(@RequestParam String department) {
        return facultyService.getDepartment(department);
    }

    /**
     * 根据房间号查询单个教职工信息
     *
     * @param room_number
     * @return
     */
    @GetMapping("/selectByRoomNumber")
    public List<Faculty> getRoomNumber(@RequestParam String room_number) {
        return facultyService.getRoomNumber(room_number);
    }

    /**
     * 根据楼栋号查询单个教职工信息
     *
     * @param building
     * @return
     */
    @GetMapping("/selectByBuilding")
    public List<Faculty> getBuilding(@RequestParam String building) {
        return facultyService.getBuilding(building);
    }

    /**
     * 根据状态查询单个教职工信息
     *
     * @param status
     * @return
     */
    @GetMapping("/selectByStatus")
    public List<Faculty> getStatus(@RequestParam boolean status) {
        return facultyService.getStatus(status);
    }

    /**
     * 添加单个教职工信息
     *
     * @param faculty
     * @return
     */
    @PostMapping("/addFaculty")
    @ResponseBody
    public void addFaculty(@RequestBody Faculty faculty) {
        facultyService.addFaculty(faculty);
    }

    /**
     * 删除单个教职工信息
     *
     * @param faculty_id
     */
    @DeleteMapping("/deleteFaculty")
    @ResponseBody
    public void deleteFaculty(@RequestParam long faculty_id) {
        facultyService.deleteFaculty(faculty_id);
    }

    /**
     * 更新单个员工信息
     *
     * @param
     * @param faculty
     */
    @PutMapping("/updateFaculty/{faculty_id}")
    public String updateFaculty(@PathVariable long faculty_id, @RequestBody Faculty faculty) {
        if (faculty.getFacultyId() != faculty_id) {
            return "Failed to update faculty: ID mismatch";
        }
        int result = facultyService.updateFaculty(faculty);
        if (result > 0) {
            return "更新成功";
        } else {
            return "更新失败";
        }
    }

    /**
     * 更新单个员工状态(启动/禁用）
     * @param faculty_id
     * @param
     * @return
     */
    @PostMapping("/{faculty_id}/updateStatus")
    public void updateFacultyStatus(@PathVariable long faculty_id, @RequestBody StatusRequest statusRequest) {
        facultyService.updateStatus(faculty_id, statusRequest.getStatus());
    }

}
